
#include "SimpleElementS.h"
#include "matrix.h"

#include <iostream>
#include <stdexcept>
#include <string>
#include <fstream>
#include <ctime>
#include <cstdlib>

using namespace std;

int MatrixTest()
{
	typedef SimpleElementS Element;

	Matrix<double> md(4,4);

	cout<<"Please input the file name:"<<endl;

	string fileName;
	cin>>fileName;
	ifstream inStream(fileName.c_str());

	if(!inStream)
	{
		cout<<"Can't open the file"<<endl;
		return 1;
	}
	inStream>>md;
	cout<<md<<endl;;

	Matrix<double> tmp(md);
	try{
		tmp.invert();
		cout<<tmp<<endl;
		cout<<tmp*md<<endl;
	}catch(runtime_error& e)
	{
		cout<<e.what()<<endl;
	}

	Matrix<Element> me(3,3),meinvert(3,3);
	inStream>>me;
	inStream>>meinvert;

	cout<<me*meinvert;
	return 0;
}

int RandomInvertTest()
{
	using namespace std;
	srand((unsigned)time(NULL));
	size_t n = 38;
	Matrix<SimpleElementS> matrix(n*n,n*n);
	int count = 1;
_start:
	for (size_t i = 0;i < n*n; i++)
	{
		for (size_t j = 0; j < n*n; j++)
		{
			matrix(i,j) = rand() % 128;
		}
	}
	try{
		matrix.invert();
	}catch(exception&  e)
	{
		count++;
		cout<<e.what()<<endl;
		goto _start;
	}
	cout<<count<<endl;
	return 0;
}

int InverTest()
{
	size_t dimen = 37;
	Matrix<SimpleElementS> matrix(dimen,dimen);
	for (size_t i = 0; i < dimen; i++)
	{
		for (size_t j = 0; j < dimen; j++)
		{
			matrix(i,j) = 0;
		}
		matrix(i,i) = 1;
	}

	try{
		matrix.invert();
		cout<<"Invert ok"<<endl;
	}catch(exception& e)
	{
		cout<<e.what()<<endl;
	}
	return 0;
}

// int main()
// {
// //	return MatrixTest();
// //	return RandomInvertTest();
// 	return InverTest();
// }